/*
 * ========THE SOLMIX PROJECT=====================================
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */

package org.solmix.services.datasource;

import java.util.Map;

import org.solmix.services.criterion.ValidationEventHandler;
import org.solmix.services.data.DataSourceData;
import org.solmix.services.exception.SLXException;
import org.solmix.services.jaxb.Eoperation;
import org.solmix.services.jaxb.Tfield;

/**
 * 
 * @version $Id$
 */
public interface DataSource extends FreeResourcesHandler
{

   /**
    * this datasource name.
 * @return
 */
String getName();
   

   /**
    * DataSource provider type.
    * <p>
    * <b>NOTE:</b> this type name must the same with service filter name.like：filter = (org.solmix.service.ds.provider =
    * name)
    * 
    * @return
    */
   String getServerType();


   /**
    * add a ValidationEventHandler to ds,it is a datasource validation for usually.
    * 
    * @param handler
    */
   void addValidationEventHandler( ValidationEventHandler handler );

   ValidationEventHandler getValidationEventHandler();

   // void setDataSourceData();

   /**
    * This attribute only can initialized at {@link #init(DataSourceData) init(data)}. so no <code>Set</code> method.
    * 
    * @return
    */
   DataSourceData getContext();

   DSResponse execute( DSRequest req ) throws SLXException;

   void clearState();

   /**
    * @param realFieldName
    * @param value
    * @return
    */
   boolean hasRecord(String realFieldName, Object value);

   /**
    * @param req
    * @return
    * @throws SLXException
    */
   boolean shouldAutoJoinTransaction(DSRequest req) throws SLXException;

   /**
    * @param req
    * @return
    * @throws Exception
    */
   Object getTransactionObject(DSRequest req) throws SLXException;

   /**
    * @return
    * @throws SLXException
    */
   String getTransactionObjectKey() throws SLXException;

   /**
    * custom transform field value,If you want to transform field value satisfied yourself purpose,you can override the
    * method in sub class.
    * 
    * @param field
    * @param obj
    * @return
    */
   Object transformFieldValue(Tfield field, Object obj);

   /**
    * return Datasource Before-processor,if no set return the default processor.Called by DataSource processor.
    * @param data
    * @return
    * @throws SLXException
    */
   DataSource instance( DataSourceData data ) throws SLXException;


   /**
    * @param value
    * @param field
    * @return
    */
   String escapeValue( Object value, Object field );

   /**
    * filter values
    * 
    * @param data
    * @return
    */
   Map<Object, Object> getProperties(Object data);

   /**
    * @param id
    * @return
    * @throws Exception
    */
   Map fetchById(Object id) throws Exception;

   /**
    * @param whereStructure
    * @return
    */
   boolean isAdvancedCriteria(Map whereStructure);


   /**
    * @param data
    * @throws SLXException
    */
   void init(DataSourceData data) throws SLXException;

   /**
    * @param context
    */
   void setContext(DataSourceData context);

   /**
    * @return
    */
   DataSourceGenerator getDataSourceGenerator();

   /**
    * @param dataSourceGenerator
    */
   void setDataSourceGenerator(DataSourceGenerator dataSourceGenerator);

   /**
    * @param req
    * @return
    * @throws SLXException
    */
   DSResponse validateDSRequest(DSRequest req) throws SLXException;

   /**
    * @param _opType
    * @return
    */
   String getAutoOperationId(Eoperation _opType);

}
